package com.atxiaodei.nio.nioday01;

import org.junit.Test;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @author wangmeng
 * @date 2020/9/24
 **/
public class LvshaqiTest排序 {

    // list<Map<String,Object>> list    List<User>

    @Test
    public void test1(){
        long l = System.currentTimeMillis();
        Map<String,Object> map=new HashMap<>();
        map.put("name", "西安");
        map.put("mom", 789);
        Map<String,Object> map1=new HashMap<>();
        map1.put("name", "深圳");
        map1.put("mom", 121223);
        Map<String,Object> map2=new HashMap<>();
        map2.put("name", "广州");
        map2.put("mom", 456);
        Map<String,Object> map3=new HashMap<>();
        map3.put("name", "富平");
        map3.put("mom", 852);
        Map<String,Object> map4=new HashMap<>();
        map4.put("name", "成都");
        map4.put("mom", 99);
        Map<String,Object> map5=new HashMap<>();
        map5.put("name", "AA");
        map5.put("mom", 423);
        Map<String,Object> map6=new HashMap<>();
        map6.put("name", "BB");
        map6.put("mom", 423);
        Map<String,Object> map8=new HashMap<>();
        map8.put("name", "武汉");
        map8.put("mom", 1789);
        List<Map<String, Object>> maps = Arrays.asList(map, map1, map2, map3, map4, map5, map6, map8);

        List<Map<String, Object>> collect = maps.stream()
                .sorted((x, y) -> -Integer.compare(Integer.parseInt(x.get("mom").toString()), Integer.parseInt(y.get("mom").toString())))
                .collect(Collectors.toList());
        collect.forEach(x->{
            System.out.println(x);
        });
        long end = System.currentTimeMillis();
        System.out.println("花费时间"+(end - l));

        System.out.println("---------------------------------------------");
        long es = System.currentTimeMillis();
        List<Map<String, Object>> collect1 = maps.stream()
                                            .parallel()
                .sorted((lvshaqi, wangmeng) -> {
                    if(lvshaqi.get("mom").toString()==wangmeng.get("mom").toString()){
                        return lvshaqi.get("name").toString().compareTo(wangmeng.get("mom").toString());
                    }else{
                        return -Integer.compare(Integer.parseInt(lvshaqi.get("mom").toString()), Integer.parseInt(wangmeng.get("mom").toString()));
                    }
                })
                .collect(Collectors.toList());
        collect.forEach(x->{
            System.out.println(x);
        });
        long ends = System.currentTimeMillis();
        System.out.println("花费时间"+(ends - es));

    }

    @Test
    public void test2(){
        long l = System.currentTimeMillis();
        Map<String,Object> map=new HashMap<>();
        map.put("name", "西安");
        map.put("mom", 789.23);
        Map<String,Object> map1=new HashMap<>();
        map1.put("name", "深圳");
        map1.put("mom", 121223.23);
        Map<String,Object> map2=new HashMap<>();
        map2.put("name", "广州");
        map2.put("mom", 456.23);
        Map<String,Object> map3=new HashMap<>();
        map3.put("name", "富平");
        map3.put("mom", 852.23);
        Map<String,Object> map4=new HashMap<>();
        map4.put("name", "成都");
        map4.put("mom", 99.23);
        Map<String,Object> map5=new HashMap<>();
        map5.put("name", "AA");
        map5.put("mom", 423.23);
        Map<String,Object> map6=new HashMap<>();
        map6.put("name", "BB");
        map6.put("mom", 423.23);
        Map<String,Object> map8=new HashMap<>();
        map8.put("name", "武汉");
        map8.put("mom", 123.12);

        List<Map<String, Object>> list = Arrays.asList(map, map1, map2, map3, map4, map5, map6, map6, map8);

//        list.forEach(System.out::println);

        long strat = System.currentTimeMillis();
        // 第一种写法
      /*  list.stream()
                .parallel().sorted((lvshaqi, wangmeng) ->
                        - new BigDecimal(lvshaqi.get("mom") == null ? "0" : lvshaqi.get("mom").toString())
                                .compareTo(new BigDecimal(wangmeng.get("mom") == null ? "0" : wangmeng.get("mom").toString())))
                .collect(Collectors.toList())
                .forEach(System.out::println);*/
        // 第二种写法
        /*list.sort((lvshaqi, wangmeng) ->
                - new BigDecimal(lvshaqi.get("mom") == null ? "0" : lvshaqi.get("mom").toString())
                        .compareTo(new BigDecimal(wangmeng.get("mom") == null ? "0" : wangmeng.get("mom").toString())));
        list.forEach(System.out::println);
        long end = System.currentTimeMillis();
        System.out.println("排序花费时间"+(end-strat));*/
        // 第三种写法
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                return -Double.compare(Double.parseDouble(o1.get("mom").toString()), Double.parseDouble(o2.get("mom").toString()));
            }
        });
        list.forEach(System.out::println);
        // 第四种写法
        Collections.sort(list,(x,y) ->
                -Double.compare(Double.parseDouble(x.get("mom").toString()), Double.parseDouble(y.get("mom").toString())));

        // 建议用  第 2 3 4 第一种算法在大数据的算法中对硬件要求很高
    }

    @Test
    public void test3(){
        Map<String,Object> map0=new HashMap<>();
        map0.put("name", "西安");
        map0.put("mom", 789.23);
        Map<String,Object> map1=new HashMap<>();
        map1.put("name", "深圳");
        map1.put("mom", 121223.23);
        Map<String,Object> map2=new HashMap<>();
        map2.put("name", "广州");
        map2.put("mom", 456.23);
        Map<String,Object> map3=new HashMap<>();
        map3.put("name", "富平");
        map3.put("mom", 852.23);
        Map<String,Object> map4=new HashMap<>();
        map4.put("name", "成都");
        map4.put("mom", 99.23);
        Map<String,Object> map5=new HashMap<>();
        map5.put("name", "AA");
        map5.put("mom", 423.23);
        Map<String,Object> map6=new HashMap<>();
        map6.put("name", "BB");
        map6.put("mom", 423.23);
        Map<String,Object> map8=new HashMap<>();
        map8.put("name", "武汉");
        map8.put("mom", 123.12);

        List<Map<String, Object>> list = Arrays.asList(map0, map1, map2, map3, map4, map5, map6, map6, map8);
        List<Map<String, Object>> list1 = Arrays.asList(map0, map1, map2, map3, map4, map5, map6, map6, map8);
        List<Map<String, Object>> list2 = Arrays.asList(map0, map1, map2, map3, map4, map5, map6, map6, map8);
        List<Map<String, Object>> list3 = Arrays.asList(map0, map1, map2, map3, map4, map5, map6, map6, map8);
        List<Map<String, Object>> list4 = Arrays.asList(map0, map1, map2, map3, map4, map5, map6, map6, map8);
        List<Map<String, Object>> list5 = Arrays.asList(map0, map1, map2, map3, map4, map5, map6, map6, map8);
        List<Map<String, Object>> list6 = Arrays.asList(map0, map1, map2, map3, map4, map5, map6, map6, map8);
        List<Map<String, Object>> list7 = Arrays.asList(map0, map1, map2, map3, map4, map5, map6, map6, map8);

        Map<String, List<Map<String, Object>>> mapA = new LinkedHashMap<>();
        mapA.put("A",list);
        Map<String, List<Map<String, Object>>> mapB = new LinkedHashMap<>();
        mapB.put("B",list1);
        Map<String, List<Map<String, Object>>> mapC = new LinkedHashMap<>();
        mapC.put("C",list2);
        Map<String, List<Map<String, Object>>> mapD = new LinkedHashMap<>();
        mapD.put("D",list3);
        Map<String, List<Map<String, Object>>> mapE = new LinkedHashMap<>();
        mapE.put("E",list4);
        Map<String, List<Map<String, Object>>> mapF = new LinkedHashMap<>();
        mapF.put("F",list5);
        Map<String, List<Map<String, Object>>> mapG = new LinkedHashMap<>();
        mapG.put("G",list6);
        Map<String, List<Map<String, Object>>> mapH = new LinkedHashMap<>();
        mapH.put("H",list7);
        List<Map<String, List<Map<String, Object>>>> newList=Arrays.asList(mapA,mapB,mapC,mapD,mapE,mapF,mapG,mapH);

        long es = System.currentTimeMillis();
        List<Map<String, List<Map<String, Object>>>> newList1=new LinkedList<>();
        newList.stream()
                .forEach(new Consumer<Map<String, List<Map<String, Object>>>>() {
                    @Override
                    public void accept(Map<String, List<Map<String, Object>>> stringListMap) {
                        stringListMap.entrySet()
                                .stream()
                                .forEach(new Consumer<Map.Entry<String, List<Map<String, Object>>>>() {
                                    @Override
                                    public void accept(Map.Entry<String, List<Map<String, Object>>> stringListEntry) {
                                        List<Map<String, Object>> collect = stringListEntry.getValue().stream()
                                                .sorted((lvshaqi, wangmeng) ->
                                                        -new BigDecimal(lvshaqi.get("mom") == null ? "0" : lvshaqi.get("mom").toString())
                                                                .compareTo(new BigDecimal(wangmeng.get("mom") == null ? "0" : wangmeng.get("mom").toString())))
                                                .collect(Collectors.toList());
                                        Map<String, List<Map<String, Object>>> mapY = new HashMap<>();
                                        mapY.put(stringListEntry.getKey(),collect);

                                        newList1.add(mapY);
                                    }
                                });

                    }
                });
        long er = System.currentTimeMillis();
        newList1.stream().forEach(System.out::println);


        System.out.println("花费时间："+(er-es));




    }
}
